package com.base.baseweb;

import com.alibaba.fastjson2.JSONObject;

import java.util.List;

public class VirtualFetchPostProcessorChain implements FetchPostProcessorChain {
    private final List<? extends FetchPostProcessor> additionalFilters;
    private int currentPosition = 0;

    public VirtualFetchPostProcessorChain(List<? extends FetchPostProcessor> additionalFilters) {
        this.additionalFilters = additionalFilters;
    }

    @Override
    public void doPostProcess(List<JSONObject> in, VariableHolder<List<JSONObject>> out) {
        if (this.currentPosition == this.additionalFilters.size()) {
            out.value = in;
        } else {
            ++this.currentPosition;
            FetchPostProcessor nextPostProcessor = this.additionalFilters.get(this.currentPosition - 1);
            nextPostProcessor.process(in, out, this);
        }
    }

    public void reset() {
         this.currentPosition = 0;
    }
}
